package com.spider.moudle.producer;

import com.spider.moudle.api.Crawler;
import com.spider.moudle.entity.Setting;
import com.spider.moudle.pool.CrawlerPool;

/**
 * Created by Linbo Hu on 2017/12/18.
 * 一个标准的生产者
 */
public class StandardProducer implements Runnable {

    /**
     * 每个网站单独的配置
     */
    private Setting setting;

    private CrawlerPool crawlerPool = CrawlerPool.getInstance();

    public StandardProducer(Setting setting) {
        this.setting = setting;
    }

    /**
     * 生成任务处理
     * @param
     */
    @Override
    public void run() {
        try {
            while (!Thread.interrupted()) {
                Crawler crawler = null;
                try {
                    crawler = crawlerPool.fetchCrawler(setting.getSite(), 1000);
                    while (null == crawler) {
                        crawler = crawlerPool.fetchCrawler(setting.getSite(), 1000);
                    }
                    crawler.generateTask();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    crawlerPool.releaseCrawler(setting.getSite(), crawler);
                }
                Thread.sleep(1000 * 60 * setting.getInterval_minutes());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
